Return to start page
Core/Maths/Library Point.j
1 library ALibraryCoreMathsPoint
2
3 /// @return Returns the z value of point with coordinates @param x and @param y.
4 function GetTerrainZ takes real x, real y returns real
5 local location usedLocation = Location(x, y)
6 local real z = GetLocationZ(usedLocation)
7 call RemoveLocation(usedLocation)
8 set usedLocation = null
9 return z
10 endfunction
11
12 /// Copied from the new FPS mod.
13 function GetPointZ takes real x, real y returns real
14 local real x1 = x - ModuloReal(x, 128.0) //top left corner
15 local real y1 = y - ModuloReal(x, 128.0) + 128.0
16 local real z1 = GetTerrainZ(x1, y1)
17 local real x3 = x1 + 128.0 //bottom right
18 local real y3 = y1 - 128.0
19 local real z3 = GetTerrainZ(x3, y3)
20 local real x2
21 local real y2
22 local real z2
23 local real A
24 local real B
25 local real C
26 local real D
27
28 if ((x-x1) < (y1-y)) then //in bottom left section
29 set x2 = x1
30 set y2 = y3
31 else
32 set x2 = x3 //top right corner
33 set y2 = y1
34 endif
35 set z2 = GetTerrainZ(x2, y2)
36
37 set A = y1*(z2 - z3) + y2*(z3 - z1) + y3*(z1 - z2) //
38
39 //http://local.wasp.uwa.edu.au/~pbourke/geometry/planeeq/ ftw
40 set B = z1 * (x2 - x3) + z2 * (x3 - x1) + z3 * (x1 - x2)
41 set C = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)
42 set D = -(x1 * (y2 * z3 - y3 * z2) + x2 * (y3 * z1 - y1 * z3) + x3 * (y1 * z2 - y2 * z1))
43
44 return -(A * x + B * y + D) / C
45 endfunction
46
47 /**
48 * Uses z values, too.
49 * @return Returns the distance between two points.
50 */
51 function GetDistanceBetweenPoints takes real x0, real y0, real z0, real x1, real y1, real z1 returns real
52 local real distanceX = (x1 - x0)
53 local real distanceY = (y1 - y0)
54 local real distanceZ = (z1 - z0)
55 return SquareRoot((distanceX * distanceX) + (distanceY * distanceY) + (distanceZ * distanceZ))
56 endfunction
57
58 /**
59 * Z has to be ignored since you're unable to create a location with its z value.
60 * @return Returns the centre between two points by using their coordinates.
61 */
62 function GetCentreBetweenPoints takes real x0, real y0, real x1, real y1 returns location
63 return Location(((x0 + x1) / 2.0), ((y0 + y1) / 2.0))
64 endfunction
65
66 /**
67 * @return Returns x value of a polar projection.
68 * @see GetPolarProjectionY
69 * @see GetPolarProjectionOfPoint
70 */
71 function GetPolarProjectionX takes real x, real angle, real distance returns real
72 return (x + distance * Cos(angle * bj_DEGTORAD))
73 endfunction
74
75 /**
76 * @return Returns y value of a polar projection.
77 * @see GetPolarProjectionX
78 * @see GetPolarProjectionOfPoint
79 */
80 function GetPolarProjectionY takes real Y, real angle, real distance returns real
81 return (Y + distance * Sin(angle * bj_DEGTORAD))
82 endfunction
83
84 /**
85 * @return Returns the polar projection location of a point by using its coordinates.
86 * @see GetPolarProjectionX
87 * @see GetPolarProjectionY
88 * @see PolarProjectionBJ
89 */
90 function GetPolarProjectionOfPoint takes real x, real y, real angle, real distance returns location
91 local real resultX = (x + distance * Cos(angle * bj_DEGTORAD))
92 local real resultY = (y + distance * Sin(angle * bj_DEGTORAD))
93 return Location(resultX, resultY)
94 endfunction
95
96 /**
97 * @return Returns angle between to points by using their coordinates.
98 * @see GetAngleBetweenPointsFromCentre
99 * @see AngleBetweenPoints
100 */
101 function GetAngleBetweenPoints takes real x0, real y0, real x1, real y1 returns real
102 return Atan2BJ((y1 - y0), (x1 - x0))
103 endfunction
104
105 /**
106 * @return Returns angle between to points by using their coordinates and a centre.
107 * @see GetAngleBetweenPoints
108 */
109 function GetAngleBetweenPointsFromCentre takes real centreX, real centreY, real x0, real y0, real x1, real y1 returns real
110 local real ax = (x0 - centreX)
111 local real ay = (y0 - centreY)
112 local real bx = (x1 - centreX)
113 local real by = (y1 - centreY)
114 local real a = SquareRoot((ax * ax) + (ax * ax))
115 local real b = SquareRoot((bx * bx) + (by * by))
116 return AcosBJ(((ax * bx) + (ay * by)) / (a * b))
117 endfunction
118
119 endlibrary